<?php
/**
 * BootAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using BootAdmin.
 *
 * @Author G.r<root@BootAdmin.com>
 */

declare (strict_types=1);

namespace app\admin\logic;

use app\common\logic\BaseLogic;
use app\common\model\DictData;
use think\db\exception\DbException;

/**
 * 字典数据逻辑
 */
class DictDataLogic extends BaseLogic
{
    protected DictData $model;

    /**
     * 初始化
     */
    public function __construct()
    {
        $this->model = new DictData();
    }

    /**
     * 获取列表
     */
    public function list($params, bool $onlyTrashed = false)
    {
        return $this->model->setParams($params)->getPage($onlyTrashed);
    }

    /**
     * 快捷列表
     */
    public function getList($params, bool $onlyTrashed = false)
    {
        return $this->model->setParams($params)->getList($onlyTrashed);
    }

    /**
     * 详情
     */
    public function read($id)
    {
        try {
            return $this->model->find($id);
        } catch (DbException $e) {
            $this->throw($e->getMessage());
        }
    }

    /**
     * 新增数据
     * @param $params
     * @return bool
     */
    public function add($params)
    {
        $result = $this->model->setParams($params)->add();
        if (!$result) {
            $this->throw($this->model->getError());
        }
        return $result;
    }

    /**
     * 修改数据
     * @param $params
     * @return bool
     */
    public function edit($params)
    {
        $result = $this->model->setParams($params)->edit($params['id']);
        if (!$result) {
            $this->throw($this->model->getError());
        }
        return $result;
    }

    /**
     * 删除数据
     * @param $id
     * @return bool
     */
    public function del($id)
    {
        $result = $this->model->del($id);
        if (!$result) {
            $this->throw($this->model->getError());
        }
        return $result;
    }

    /**
     * 真实删除数据
     * @param $id
     * @return bool
     */
    public function realDel($id)
    {
        $result = $this->model->realDel($id);
        if (!$result) {
            $this->throw($this->model->getError());
        }
        return $result;
    }

    /**
     * 恢复数据
     * @param $id
     * @return bool
     */
    public function recovery($id)
    {
        $result = $this->model->recovery($id);
        if (!$result) {
            $this->throw($this->model->getError());
        }
        return $result;
    }

    /**
     * 字段修改
     * @param $params
     * @return bool
     */
    public function modify($params)
    {
        $result = $this->model->setParams($params)->modify($params['id']);
        if (!$result) {
            $this->throw($this->model->getError());
        }
        return $result;
    }
}